import {BaseService} from '../base.service'
import Wrapper from "../util";
import {getVerifyId} from "../auth/auth.service";

const wrapper = new Wrapper;

export class ReleasesService extends BaseService {
  static get entity() {
    return 'releases'
  }

  static getByCode(code, parameters = {}, isShowNotification = true) {
    const params = {
      ...parameters,
    }
    return new Promise((resolve, reject) => {
      return this.request({auth: false}).get(`${this.entity}/code/${code}`, {params}).then(response => {
        wrapper.setWrapper(response, isShowNotification).then(() => {
          return resolve(response.data)
        }).catch(() => {
          return reject(response.data)
        })
      }).catch(error => {
        reject(console.log('getById ' + this.entity, error))
      })
    })
  }

  static generateCode(id, parameters = {}, isShowNotification = true) {
    const params = {
      ...parameters,
    }
    let verify_id = '?verify_id=' + getVerifyId();
    return new Promise((resolve, reject) => {
      return this.request({auth: true}).get(`${this.entity}/generateCode/${id}${verify_id}`, {params}).then(response => {
        wrapper.setWrapper(response, isShowNotification).then(() => {
          return resolve(response.data)
        }).catch(() => {
          return reject(response.data)
        })
      }).catch(error => {
        reject(console.log('getById ' + this.entity, error))
      })
    })
  }

  static updateByCode(data, code, isShowNotification = true) {
    return new Promise((resolve, reject) => {
      return this.request({auth: false}).put(`${this.entity}/code/${code}`, data).then(response => {
        wrapper.setWrapper(response, isShowNotification).then(() => {
          return resolve(response.data)
        }).catch(() => {
          return reject(response.data)
        })
      }).catch(error => {
        reject(console.log('update ' + this.entity, error))
      })
    })
  }
}

export class ReleasesTypesService extends BaseService {
  static get entity() {
    return 'releases/types'
  }

  static index(parameters = {}, isShowNotification = true) {
    const params = {
      ...parameters,
    }
    return new Promise((resolve, reject) => {
      return this.request({auth: false}).get(`${this.entity}`, {params}).then(response => {
        wrapper.setWrapper(response, isShowNotification).then(() => {
          return resolve(response.data)
        }).catch(() => {
          return reject(response.data)
        })
      }).catch(error => {
        reject(console.log('getList ' + this.entity, error))
      })
    })
  }
}


export class ReleasesGenresService extends BaseService {
  static get entity() {
    return 'releases/genres'
  }

  static index(parameters = {}, isShowNotification = true) {
    const params = {
      ...parameters,
    }
    return new Promise((resolve, reject) => {
      return this.request({auth: false}).get(`${this.entity}`, {params}).then(response => {
        wrapper.setWrapper(response, isShowNotification).then(() => {
          return resolve(response.data)
        }).catch(() => {
          return reject(response.data)
        })
      }).catch(error => {
        reject(console.log('getList ' + this.entity, error))
      })
    })
  }
}


export class ReleasesTracksService extends BaseService {
  static get entity() {
    return 'releases/tracks'
  }

  static updateByCode(data, code, isShowNotification = true) {
    return new Promise((resolve, reject) => {
      return this.request({auth: false}).put(`${this.entity}/code/${code}`, data).then(response => {
        wrapper.setWrapper(response, isShowNotification).then(() => {
          return resolve(response.data)
        }).catch(() => {
          return reject(response.data)
        })
      }).catch(error => {
        reject(console.log('update ' + this.entity, error))
      })
    })
  }
}


